import 'package:flutter/material.dart';
import 'package:flutter/cupertino.dart';
import 'package:flutter_spinkit/flutter_spinkit.dart';
import '../../spaces/image_space/record_image_add.dart';
import '../../helper/routeHelper.dart';
import '../../model/recording.dart';
import 'record_card.dart';

typedef RecordDeleteCallback = void Function(Recording r1);
typedef RecordAddCallback = void Function(Recording r1);

class RecordListPage extends StatefulWidget {
  @override
  RecordListPage(this.SpaceId, this.IfCanAdd);
  final String SpaceId;
  final bool IfCanAdd;

  @override
  RecordListPageState createState() => new RecordListPageState();
}

class RecordListPageState extends State<RecordListPage> {
  List<Recording> RList = <Recording>[];

  bool firstLoading = true;
  ScrollController _scrollController = new ScrollController();
  bool isEnd = false;

  @override
  initState() {
    super.initState();

    _scrollController.addListener(() {
      if (_scrollController.position.pixels ==
          _scrollController.position.maxScrollExtent) {
        getMore();
      }
    });

    getNewer();
  }

  Future<Null> getMore() async {
    // await addOne();
    int count = 5;
    if (isEnd == true) return;
    int page = RList.length ~/ count;
    var list1 = await Recording.GetList(widget.SpaceId, 1, 5);
    var list2 = list1
        .where((p) => RList.any((x) => x.ImgId == p.ImgId) == false)
        .toList();

    setState(() {
      if (list1.length < count) isEnd = true;
      RList.insertAll(RList.length, list2);
    });
  }

  onDeleted(Recording r1) => {
        setState(() {
          RList.remove(r1);
        })
      };

  onAdded(Recording r1) => {
        setState(() {
          RList.insert(0, r1);
        })
      };

  Future<Null> getNewer() async {
    var list1 = await Recording.GetList(widget.SpaceId, 1, 5);
    if (firstLoading == true) {
      setState(() {
        firstLoading = false;
      });
    }

    if (list1.length == 0) return;

    var list2 = list1
        .where((p) => RList.any((x) => x.ImgId == p.ImgId) == false)
        .toList();

    if (list2.length == 0) return;

    list2.insertAll(list2.length, RList);

    setState(() {
      RList = list2;
    });
  }

  buildListData(BuildContext context, int index) {
    var x = RList[index];

    if (x.RType == 1) return RecordCard.ImgCard(context, x, onDeleted);
    //if (x.RType == 2) return RecordCard.AudioCard(context,x);
    return new Container();
  }

  @override
  Widget build(BuildContext context) {
    // TODO: implement build

    return Scaffold(
      // appBar: AppBar(title: Text("照片列表"),),
      body: firstLoading == true
          ? Center(
              child: SpinKitCircle(
              color: Colors.blue,
            ))
          : new RefreshIndicator(
              child: ListView.builder(
                itemCount: RList.length + 1,
                itemBuilder: (context, index) {
                  if (index != RList.length) {
                    return buildListData(context, index);
                  } else {
                    if (isEnd == true) {
                      return Container(
                          margin: EdgeInsets.all(12.0),
                          child: Center(
                              child: Text(
                            "没有更多了",
                            style: TextStyle(color: Colors.grey),
                          )));
                    } else {
                      return new Container();
                    }
                  }
                },
                controller: _scrollController,
              ),
              onRefresh: getNewer,
            ),
      //floatingActionButton:widget.IfCanAdd? RecordCard.MenuButton(context, widget.SpaceId):null,
      //floatingActionButtonLocation: fabMenuLocation,
      floatingActionButton: FloatingActionButton(
        child: Icon(Icons.add),
        onPressed: () {
          RouteHelper.ShowPage(
              context, ImgRecordAddPage(widget.SpaceId, (s) => {}));
        },
      ),
    );
  }
}
